package org.adultedu.controller;

import org.adultedu.pojo.Student;
import org.adultedu.pojo.Teacher;
import org.adultedu.service.StudentService;
import org.adultedu.service.TeacherService;
import org.adultedu.utils.LoginToken;
import org.adultedu.utils.RandomNumberGenerator;
import org.adultedu.utils.UUIDUtils;
import org.adultedu.pojo.User;
import org.adultedu.result.Result;
import org.adultedu.result.ResultEnum;
import org.adultedu.result.ResultUtil;
import org.adultedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @GetMapping(value = "/login")
    public Result UserLogin(String username,String password,String roleid){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRoleid(Integer.parseInt(roleid));
        System.out.println(user);
        Map<String,String> map = userService.UserLogin(username);
        System.out.print(map);
        if(user.getPassword().equals(map.get("password")) && user.getRoleid().equals(map.get("roleid")) && !map.get("state").equals("0")){
            LoginToken res = new LoginToken();
            res.setName(user.getUsername());
            res.setToken(user.getPassword());
            res.setRoleid(user.getRoleid());
            res.setUid(map.get("id"));
            System.out.println("查询成功");
            return ResultUtil.success(res);
        }else if(map.get("state").equals("0")) {
            return ResultUtil.error(400, "你的账号因为" + map.get("reason") + "被封禁");
        }else {
            System.out.println("查询失败");
            return ResultUtil.error(ResultEnum.VERIFY_FAIL.getCode(),ResultEnum.VERIFY_FAIL.getMsg());
        }
    }

    @GetMapping(value = "/adminlogin")
    public Result adminLgoin(String username,String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);
        Map<String,Object> map = userService.UserLogin(username);
        System.out.print(map);
        if(user.getPassword().equals(map.get("password")) && !map.get("state").equals("0")){
            LoginToken res = new LoginToken();
            res.setName(user.getUsername());
            res.setToken(user.getPassword());
            res.setUid((String)map.get("id"));
            res.setRoleid((Integer) map.get("roleid"));
            System.out.println("查询成功");
            return ResultUtil.success(res);
        }else if(map.get("state").equals("0")) {
            return ResultUtil.error(400, "你的账号暂不可用");
        }else {
            System.out.println("查询失败");
            return ResultUtil.error(ResultEnum.VERIFY_FAIL.getCode(),ResultEnum.VERIFY_FAIL.getMsg());
        }
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Result UserRegister(@RequestBody Map<String,Object> map){
        User user = new User();
        Integer roleid = (Integer) map.get("roleid");
        String uuid = UUIDUtils.generateShortUuid();
        user.setId(uuid);
        user.setUsername((String)map.get("username"));
        user.setPassword((String)map.get("password"));
        user.setRoleid(roleid);
        System.out.println(user);
        if(userService.queryUserByUsername(user.getUsername())==null){
            userService.insertUser(user);
            userService.insertRole(uuid,roleid);
            switch (roleid){
                case 1:
                {
                    Student s = new Student();
                    s.setSid(uuid);
                    s.setTele(user.getUsername());
                    studentService.insertStudent(s);
                    break;
                }
                case 2:{
                    Teacher t = new Teacher(uuid,null,null,null,null,null,null,user.getUsername(),null,null,null,null);
                    teacherService.insertTeacher(t);
                    break;
                }
                default:{

                }
            }
            System.out.println("插入成功");
            return ResultUtil.success(uuid);
        }
        else {
            System.out.println("用户以存在请更换用户名");
            return ResultUtil.error(ResultEnum.REGISTER_FAIL.getCode(),ResultEnum.REGISTER_FAIL.getMsg());
        }
    }
    @GetMapping(value = "/getadminlist")
    public Result getAdmin(String username){
        List list = userService.queryAdmin(username);
        if(!list.isEmpty()){
            return ResultUtil.success(list);
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value="/editAdmin",method = RequestMethod.POST)
    public Result editAdmin(@RequestBody Map<String,Object> map){
        System.out.println(map);
        String id = (String) map.get("id");
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Integer roleid =  (Integer)map.get("roleid");
        Integer flag = (Integer) map.get("flag");
        System.out.println(flag);
        System.out.println(id+"-"+username+"-"+password+"-"+roleid);
        if(flag == 2){
            int i = userService.updateAdmin(id,username,password,null,null,null);
            int j = userService.updateRole(id,roleid);
            if(i==1 && j==1){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
            }
        }else{
            String uuid = UUIDUtils.generateShortUuid();
            int i = userService.insertAdmin(uuid,username,password,roleid);
            int j = userService.insertRole(uuid,roleid);
            if(i==1 && j==1){
                return ResultUtil.success();
            }else {
                return ResultUtil.error(ResultEnum.INSERT_FAIL.getCode(),ResultEnum.INSERT_FAIL.getMsg());
            }
        }
    }

    @RequestMapping(value="/insertAdmin",method = RequestMethod.POST)
    public Result insertAdmin(@RequestBody Map<String,Object> map) {
        System.out.println(map);
        String id = UUIDUtils.generateShortUuid();
        String username = (String)map.get("username");
        String password = (String)map.get("password");
        Integer roleid = (Integer)map.get("roleid");

        return null;
    }
    @RequestMapping(value="/disableAdmin",method = RequestMethod.POST)
    public Result diableAdmin(@RequestBody Map<String,Object> map){
        String id = (String) map.get("id");
        String reason = (String) map.get("reason");
        String other = (String) map.get("other");
        System.out.println(reason+"-"+other);
        int i = userService.updateAdmin(id,null,null,null,reason,other);
        if(i==1){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @GetMapping("/updatepwd")
    public Result updatePwd(String id,String pwd){
        int i = userService.updateAdmin(id,null,pwd,null,null,null);
        if(i==1){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }

    }

    @GetMapping(value="/updatestate")
    public Result updateState(String id,Integer state){
        int i = userService.updateAdmin(id,null,null,state,null,null);
        if(i == 1){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @GetMapping(value="/deleteAdmin")
    public Result deleteAdmin(String id){
        System.out.println(id);
        int i= userService.deleteAdmin(id);
        if(i == 1){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.DELETE_FAIL.getCode(),ResultEnum.DELETE_FAIL.getMsg());
        }
    }

}

